using System;
using System.Collections.Generic;
using MyApiApp.Api.Entity;
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using Newtonsoft.Json;
using MyApiApp.Api.ParamModel;

namespace MyApiApp.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController:ControllerBase
    {
         private MyApiApp.Api.Db.EntityDb db = new Api.Db.EntityDb();
        [HttpGet]
        public dynamic Get(){
            var users=GetUsers();
            return users;
        }

        [HttpGet("{id}")]
        public dynamic Get(int id){
            var users=GetUsers();
             var user = users.Where(x => x.Id == id).FirstOrDefault();
            return user;
        }

        [HttpPost]
        public string Post(NewUser model){
            JsonSerializerSettings settings = new JsonSerializerSettings();
            // settings.MissingMemberHandling = MissingMemberHandling.Ignore;
            settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            var user = new Users
            {
                UserName = model.UserName,
                PassWord = model.PassWord
            };

            db.Users.Add(user);
            db.SaveChanges();

            var res = new
            {
                Code = 1000,
                Data = user,
                Msg = "创建用户成功"
            };
            return JsonConvert.SerializeObject(res, settings);
        }

        [HttpPut("{id}")]
        public dynamic Put(int id,NewUser model){
            var user = db.Users.Where(x=>x.Id==id).FirstOrDefault();

            if(user != null){
                user.UserName = model.UserName;
                user.PassWord = model.PassWord;
                user.UpdatedTime=DateTime.Now;
                db.Update(user);
                db.SaveChanges();
                return new
                {
                    Code = 1000,
                    Data = user,
                    Msg = string.Format("用户{0}修改成功", id)
                };
            }
            else
            {
                return new
                {
                    Code = 104,
                    Data = "",
                    Msg = "用户不存在，请确认后重试"
                };
            }
        }

        [HttpDelete("{id}")]
        public dynamic Delete(int id){
            var user = db.Users.Where(x=>x.Id==id).FirstOrDefault();
            if(user != null){
                db.Users.Remove(user);
                db.SaveChanges();
                return new{
                    Code=1000,
                    Msg=string.Format("用户{0}删除成功",id)
                };
            }
                else{
                    return new{
                        Code=104,
                        Data="",
                        Msg="用户不存在，请确认后再试"
                    };
                }
            }
        

        public IEnumerable<Users> GetUsers(){
            var users=
                new List<Users>{
                    new Users{
                        Id=1,
                        UserName="婷婷公主",
                        PassWord="123"
                    },
                    new Users{
                        Id=2,
                        UserName="榕榕骑士",
                        PassWord="123"
                    },
                    new Users{
                        Id=3,
                        UserName="憨憨",
                        PassWord="123"
                    },
                    new Users{
                        Id=4,
                        UserName="胖胖",
                        PassWord="123"
                    }
                };
                return users;
        }
    }
}
